/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Models.DAOs;

import DBConnection.ConnectionFactory;
import Models.Products;
import Security.Encrypter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.ArrayList;
/**
 *
 * @author 0905
 */
public class Product_DAO {
    
    /**
     * Inserts a new product. Access products table in the database.
     * @param product model
     * @return int newProductID
     */
    public int addProduct(Products product){
        
        int newProductID = 0;
        try{
            
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            int i = 1;
            PreparedStatement pstmtProduct = conn.prepareStatement ("INSERT INTO products(title, synopsis, price, type) VALUES(?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
            pstmtProduct.setString(i++, product.getTitle());
            pstmtProduct.setString(i++, product.getSynopsis());
            pstmtProduct.setFloat(i++, product.getPrice());
            pstmtProduct.setString(i++, product.getType());
            pstmtProduct.executeUpdate();
            
            ResultSet rs = pstmtProduct.getGeneratedKeys();
            if (rs!= null && rs.next()) {
                newProductID = rs.getInt(1);
            }
            return newProductID;            
        }
        catch(Exception e){
            return newProductID;
        }
    }
    
    public boolean updateProduct(Products product){
        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            int i = 1;
            
            PreparedStatement pstmtProduct = conn.prepareStatement ("UPDATE products SET title = ?, synopsis = ?, price = ? WHERE productID = ?");
            pstmtProduct.setString(i++, product.getTitle());
            pstmtProduct.setString(i++, product.getSynopsis());
            pstmtProduct.setFloat(i++, product.getPrice());   
            pstmtProduct.setInt(i++, product.getProductID());
            pstmtProduct.executeUpdate();
            return true;
        }
        catch(Exception e){
        
            return false;
        }
    }
    
        public ArrayList<Products> getsearchedProduct(){
        
        Products product = null;
        ArrayList<Products> productList = null;
        
        try{
        
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            int i = 1;
			
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM PRODUCTS WHERE title LIKE %?%");
            pstmt.setString(i++, product.getTitle());
			
            ResultSet rs = pstmt.executeQuery();
            productList = new ArrayList<Products>();
            Encrypter encrypt = new Encrypter();
			
            while(rs.next())
            {
                product = new Products();
                
                //System.out.println(encrypt.encrypt());
                product.setProductCrypt(encrypt.encrypt(Integer.toString(rs.getInt("productID"))));
                //String str  = Integer.toString(rs.getInt("prodcutID"));
                //System.out.println(str);
                
                product.setProductID(rs.getInt("productID"));
                product.setTitle(rs.getString("title"));
                product.setSynopsis(rs.getString("synopsis"));
                product.setPrice(rs.getFloat("price"));
                System.out.println(Integer.toString(rs.getInt("productID")));
                productList.add(product);
            }
            
            return productList;
        }
        catch(SQLException e){
        
            return productList;
        }
            
    }
    
    public boolean productStatus(String productCode){
        try{
            int productID = Integer.parseInt(new Encrypter().decrypt(productCode));
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            int i = 1;
            System.out.println(String.valueOf(productID));
            PreparedStatement pstmtProduct = conn.prepareStatement ("UPDATE products SET status = ? WHERE productID = ?");
            pstmtProduct.setInt(i++, 0);
            pstmtProduct.setInt(i++, productID);
            pstmtProduct.executeUpdate();
            return true;
        }
        catch(Exception e){
        
            return false;
        }
    }
    public boolean updateSupply(Products product){
        try{
            
            return true;
        }
        catch(Exception e){
            
            return false;
        }
    }
    
    
}
